﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using FluentFTP;

namespace FtpDownDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("请输入Ftp服务器地址（例：127.0.0.1）");
                var server = Console.ReadLine();
                Console.WriteLine("请输入Ftp用户名（例：root）");
                var uname = Console.ReadLine();
                Console.WriteLine("请输入Ftp用户密码（例：123123）");
                var pass = Console.ReadLine();
                Console.WriteLine("请输入要下载的文件路径（例：/htdocsdir/todownload.rar）");
                var path = Console.ReadLine();

                Console.WriteLine("准备开始下载喽");
                var downPath = DownLoadFile(server, uname, pass, path);
                Console.WriteLine("下载完成,文件在：{0}", downPath);
                Console.ReadLine(); 
            }

        }

        private static string DownLoadFile(string server, string uname, string pass, string path)
        {
            var client = new FtpClient(server);
            try
            {
                client.Credentials= new NetworkCredential(uname, pass);
                client.Connect();
                var name = System.IO.Path.GetFileName(path);
                client.DownloadFile("./" + name, path);
                client.Disconnect();
                return name;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
//                throw;
            }

            return null;
        }
    }
}
